/**
*  \file paragen.h
*
*  \brief   Parameter Generator
*
*  \author  Michael J. Hohmann <michael.hohmann@linde-mh.de>
*  \author  Joerg.Treisch      <joerg.treisch@linde-mh.de>
*
*/

#ifndef PARAGEN_H_INCLUDED
#define PARAGEN_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../sd/sd.h"
#include "../cfg/paragen_cfg.h"
/*================================================[ public defines   ]================================================*/

#define HAEDER_VALID_OFFSET              0
#define HAEDER_CHECKSUM_OFFSET           4
#define HAEDER_CHECKSUM_INV_OFFSET       8
#define HAEDER_FINGER_OFFSET             12
#define HAEDER_OFFSET                    28


/*================================================[ public datatypes ]================================================*/

/** Paragen Types.
*
*   This types are supported by the paragen framework.
*
*   \attention The use of this enumerators is only expected by the automatic generated code.
*/
enum paragen_type
{
    paragen_type_u8_mm,
    paragen_type_u16_mm,
    paragen_type_u32_mm,
    paragen_type_s8_mm,
    paragen_type_s16_mm,
    paragen_type_s32_mm,
    paragen_type_ch_mm
#ifdef CC_PARAGEN_USE_F32
    ,paragen_type_f32_mm
#endif
};


/** Validation Header
*
*  This struct will be located in the NV-RAM.
*  It contains information of the correctness of the contained data.
*
*/
struct paragen_validation_header
{
      U32 valid;              /**< indicate if struct is considered valid */
      U32 checksum ;          /**< checksum of the parameters */
      U32 checksum_invers;    /**< invers checksum of the parameters*/
      U8  fingerprint[16];    /**< fingerprint that indicate that the stored data match the structure of the current parameters  */
};



/** Type header
*
*   \attention The use of this structure is only expected by the automatic generated code.
*/
struct paragen_type_header
{
    enum paragen_type type;
    U32 offset;
    U32 readlevel;
    U32 writelevel;
};

struct paragen_index
{
    U32 index;
    struct paragen_type_header  *info;
};


/** Data in NV-RAM
*
*  It is not expected hat a instance of this struct is used is.
*  It will only referenced to calculate the offset to the data structure.
*/
struct pargen_nv_data
{
   struct paragen_validation_header validation;
   U8 parameter_data[0x400]; /**< this is the place where an arbitrarily struct is placed */
};


struct paragen_object
{
   /* RAM */
   struct sd *dev;                                       /**< storage device pointer                                              */
   void  *ram_param;                                    /**< Pointer to the parameter in RAM                                     */

   /* NV-RAM */
   U32 nv_param;                                       /**< Address of the parameters and validation information. only accessible by the storage device dev */

   /* ROM */
   struct paragen_index const *index_list;            /**< Pointer to the index structure in flash. The dimension of the array is given by parameter_total_count  */
   struct paragen_type_header const * type_info_list; /**< Pointer to parameter information in ROM. The size is stored in item_count                       */
   void const  *default_parameter;                    /**< Pointer to the default parameter set in ROM. The parameter_struct_size of the parameter is stored in parameter_struct_size      */

    /* other object information */
    U32 parameter_struct_size;                         /**< Size of the parameter struct in bytes */
    U32 parameter_total_count;                         /**< Number of parameters */
    U8  fingerprint[16];                                /**< MD5 finger print */
};

struct paragen_type_u8_mm
{
   struct paragen_type_header head;
   U8 min_value;
   U8 max_value;
   U8 increment;
};

struct paragen_type_u16_mm
{
   struct paragen_type_header head;
   U16 min_value;
   U16 max_value;
   U16 increment;
};

struct paragen_type_u32_mm
{
   struct paragen_type_header head;
   U32 min_value;
   U32 max_value;
   U32 increment;
};

struct paragen_type_s8_mm
{
   struct paragen_type_header head;
   S8 min_value;
   S8 max_value;
   S8 increment;
};

struct paragen_type_s16_mm
{
   struct paragen_type_header head;
   S16 min_value;
   S16 max_value;
   S16 increment;
};

struct paragen_type_s32_mm
{
   struct paragen_type_header head;
   S32 min_value;
   S32 max_value;
   S32 increment;
};

struct paragen_type_ch_mm
{
   struct paragen_type_header head;
   U16 min_value;                     /* mininum string parameter_struct_size */
   U16 max_value;                     /* mininum string parameter_struct_size */
   S8 increment;
};


#ifdef CC_PARAGEN_USE_F32
struct paragen_type_f32_mm
{
   struct paragen_type_header head;
   F32 min_value;
   F32 max_value;
   F32 increment;
};
#endif


/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/



/** Store parameter.
*
*
*   The function paragen_store() store the parameter that are currently stored in ram into the
*   non volatile storage.
*
*   \param obj Object pointer.
*
*   \return RC_SUCCESS
*/
RC paragen_store( struct paragen_object const  * const obj );

RC paragen_store_by_header( struct paragen_object const  * const obj, struct paragen_type_header const * p );



/** Load parameter
*  The function will load the parameter from NV-RAM.
*  It will check the validity of data (CRC), otherwise the devault parameter will stored in RAM.
*
*   \param obj Object pointer.
*/
RC paragen_load( struct paragen_object const  * const obj );


/** Retrieve parameter.
*
*   \param obj Object pointer.
*   \param index Index of the wanted parameter.
*   \param data Pointer where the parameter will be stored.
*   \param size Pointer to an U32 variable where the legth of the passed data buffer is passed
*               and the resultvalue is stored.
*   \param readlevel Current rights to read variables.
*
*   \return -RC_SUCCESS
*           -RC_ERROR_OPEN
*           -RC_ERROR_SIZE
*           -RC_ERROR_RIGHTS
*
*/
RC paragen_get( struct paragen_object const  * const obj, U32 index, void  *data, U32 *size , U32 readlevel );

/** Set a parameter.
*
*  \param obj Object pointer.
*  \param index Index of the parameter.
*  \param data Pointer to the data that should be wirtten to the parameter.
*  \param size Size of the parameter
*  \param writelevel
*
*  \return - RC_ERROR_OPEN No valid ID.
*      - RC_ERROR_SIZE Wrong size.
*      - RC_ERROR_RANGE Parameter is was not in range.
*      - RC_ERROR_RIGHTS no rights to write.
*
*/
RC paragen_set( struct paragen_object const * const obj, U32 index, void const * data, U8 size, U32  writelevel );


/** Inc/Dec a Parameter.
*
*
*   The function paragen_shift() increments/decrements the parameter in area of minimum and maximum limitm.
*
*
*  \param obj Object pointer.
*  \param index Index of the parameter.
*  \param dir direction inc /dec
*  \param writelevel
*
*  \return - RC_ERROR_OPEN No valid ID.
*      - RC_ERROR_SIZE Wrong size.
*      - RC_ERROR_RANGE Parameter is was not in range.
*      - RC_ERROR_RIGHTS no rights to write.
*
*/
RC paragen_shift(struct paragen_object const  * const obj, U32 index , S16 dir, U32 writelevel );

/** Set a parameter to default.
*
*  \param obj Object pointer.
*  \param index Index of the parameter.
*  \param data Pointer to the data that should be wirtten to the parameter.
*  \param size Size of the parameter
*  \param writelevel
*
*  \return - RC_ERROR_OPEN No valid ID.
*      - RC_ERROR_SIZE Wrong size.
*      - RC_ERROR_RANGE Parameter is was not in range.
*      - RC_ERROR_RIGHTS no rights to write.
*
*/
RC paragen_set_default( struct paragen_object const * const obj, U32 index, void* data, U8 size, U32 writelevel );



/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2012 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
